package com.xuecheng.content.api;

import org.redisson.api.RLock;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Queue;

/**
 * @version 1.0
 * @description
 * @Author Endeavor-ZQB
 * @Create 2023/2/23 15:40
 */
@RestController
@RequestMapping("/redisson")
public class RedissonTestController {

    @Autowired
    RedissonClient redissonClient;

    /**
     * 入队
     */
    @GetMapping("/joinqueue")
    public Queue<String> joinqueue(String queuer) {
        //这个队列是否存在，如果不存在则在redis中创建
        RQueue<String> queue001 = redissonClient.getQueue("queue001");
        queue001.add(queuer);
        return queue001;
    }

    /**
     * 出队
     */
    @GetMapping("/removequeue")
    public String removequeue() {

        RQueue<String> queue001 = redissonClient.getQueue("queue001");
        String queuer = queue001.remove();
        return queuer;
    }

    /**
     * 获取分布式锁
     */
    @GetMapping("/getlock")
    public void getlock(){
        RLock lock001 = redissonClient.getLock("lock001");
        //获取lock001锁
        lock001.lock();
        try {
            System.out.println("我拿到锁了.....");
            Thread.sleep(35000);
        }catch (InterruptedException e){

        }finally {
            System.out.println("我释放锁了....");
            lock001.unlock();
        }
    }
}
